package com.hemaapp.yjnh.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseNetWorker;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.activity.CmnMerchantDetailActivity;
import com.hemaapp.yjnh.activity.FarmerDetailsActivity;
import com.hemaapp.yjnh.activity.MainActivity;
import com.hemaapp.yjnh.activity.MyFarmerActivity;
import com.hemaapp.yjnh.activity.WebviewActivity;
import com.hemaapp.yjnh.activity.ZouzouCampsiteMerchantDetailActivity;
import com.hemaapp.yjnh.bean.AdImage;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.view.ImageCarouselBanner;

import java.util.ArrayList;

/**首页和个模块的头部图片轮播监听事件
 * Created by UU on 2016/9/24.
 */

public class ImageCarouselHeadClickListener implements ImageCarouselBanner.ImageCarouselListener {

    private Context mContext;
    private ArrayList<AdImage> mDatas = new ArrayList<>();
    private String AdType = "0";//广告类型	1首页广告;2模块头部广告;3易家推荐;4热门区域;5粮库的广告

    public ImageCarouselHeadClickListener(Context context, ArrayList<AdImage> datas, String type) {
        mContext = context;
        mDatas= datas;
        AdType = type;
    }
    @Override
    public void onItemClickListener(int index) {
        AdImage add = mDatas.get(index);
        String keyid = add.getKeyid();
        //保存广告点击记录
        User user = BaseApplication.getInstance().getUser();
        String token = user == null?"":user.getToken();
        new BaseNetWorker(mContext).ADRecord( token, AdType, add.getId());
        //跳转处理
        switch (Integer.parseInt(add.getKeytype())) {
            case 1://跳转到商品
                String type = add.getType();
                if (TextUtils.isEmpty(type)) {
                    return;
                }
                Intent intent = BaseUtil.createBlogIntent(mContext, type);
                intent.putExtra("blog_id", keyid);
                mContext.startActivity(intent);
                break;
            case 2: //跳转到田家/商家
                String role = add.getRole();
                if (role == null || role.length() == 0) {
                    return;
                }
                Intent itClient = new Intent();
                //0普通用户;1田家用户;2地标商家;3村游商家;4省级代理;5运营商;6乐乐商家;
                //7特产商家;8粮库商家;11走走露营商家;12走走房车商家;21苗木;22花卉;23家电;24日用商品
                if (role.equals("1")) {
                    itClient.setClass(mContext, FarmerDetailsActivity.class);
                    if (user != null) {
                        String id = user.getId();
                        if (id.equals(keyid)) {
                            itClient.setClass(mContext, MyFarmerActivity.class);
                        }
                    }
                }else if(role.equals("3") || role.equals("6") || role.equals("11")) {//走走露营
                    itClient.setClass(mContext, ZouzouCampsiteMerchantDetailActivity.class);
                }else {
                    itClient.setClass(mContext, CmnMerchantDetailActivity.class);
                }
                itClient.putExtra("client_id", keyid);
                mContext.startActivity(itClient);
                break;
            case 3://图文详情,跳转到大图片
                keyid = add.getId();
                Intent intent1 = new Intent(mContext, WebviewActivity.class);
                if (AdType.equals("1")) {//首页
                    intent1.putExtra("parm", keyid);
                    intent1.putExtra("type", 7);
                }else if (AdType.equals("2")) {
                    intent1.putExtra("parm", add.getId());
                    intent1.putExtra("type", 8);
                }else if (AdType.equals("3")) {
                    intent1.putExtra("parm", keyid);
                    intent1.putExtra("type", 9);
                }else if (AdType.equals("5")) {
                    intent1.putExtra("parm", keyid);
                    intent1.putExtra("type", 16);
                }
                mContext.startActivity(intent1);
                break;
            default:
                break;
        }
    }
}
